﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace KetBanBonPhuong
{
    /// <summary>
    /// Summary description for Profile
    /// </summary>
    public class Profile
    {

        private int profile_id;

        public int Profile_id
        {
            get
            {
                return profile_id;
            }
            set
            {
                profile_id = value;
            }
        }

        private int user_id;

        public int User_id
        {
            get
            {
                return user_id;
            }
            set
            {
                user_id = value;
            }
        }

        private int rating;

        public int Rating
        {
            get
            {
                return rating;
            }
            set
            {
                rating = value;
            }
        }

        private string nick_name;

        public string Nick_name
        {
            get
            {
                return nick_name;
            }
            set
            {
                nick_name = value;
            }
        }

        private DateTime dob;

        public DateTime Dob
        {
            get
            {
                return dob;
            }
            set
            {
                dob = value;
            }
        }

        private bool gender;

        public bool Gender
        {
            get
            {
                return gender;
            }
            set
            {
                gender = value;
            }
        }

        private string about_me;

        public string About_me
        {
            get
            {
                return about_me;
            }
            set
            {
                about_me = value;
            }
        }

        private string relationship;

        public string Relationship
        {
            get
            {
                return relationship;
            }
            set
            {
                relationship = value;
            }
        }

        private string looking_for;

        public string Looking_for
        {
            get
            {
                return looking_for;
            }
            set
            {
                looking_for = value;
            }
        }

        private string phone;

        public string Phone
        {
            get
            {
                return phone;
            }
            set
            {
                phone = value;
            }
        }

        private string interests;

        public string Interests
        {
            get
            {
                return interests;
            }
            set
            {
                interests = value;
            }
        }

        private string education;

        public string Education
        {
            get
            {
                return education;
            }
            set
            {
                education = value;
            }
        }

        private string hobbies;

        public string Hobbies
        {
            get
            {
                return hobbies;
            }
            set
            {
                hobbies = value;
            }
        }

        private DateTime modified_date;

        public DateTime Modified_date
        {
            get
            {
                return modified_date;
            }
            set
            {
                modified_date = value;
            }
        }

        public Profile()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Profile(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                profile_id = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    user_id = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    rating = sdr.GetInt32(2);
                if (!sdr.IsDBNull(3))
                    nick_name = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    dob = sdr.GetDateTime(4);
                if (!sdr.IsDBNull(5))
                    gender = sdr.GetBoolean(5);
                if (!sdr.IsDBNull(6))
                    about_me = sdr.GetString(6);
                if (!sdr.IsDBNull(7))
                    relationship = sdr.GetString(7);
                if (!sdr.IsDBNull(8))
                    looking_for = sdr.GetString(8);
                if (!sdr.IsDBNull(9))
                    phone = sdr.GetString(9);
                if (!sdr.IsDBNull(10))
                    interests = sdr.GetString(10);
                if (!sdr.IsDBNull(11))
                    education = sdr.GetString(11);
                if (!sdr.IsDBNull(12))
                    hobbies = sdr.GetString(12);
                if (!sdr.IsDBNull(13))
                    modified_date = sdr.GetDateTime(13);
            }
        }

        public static DataTable GetAll()
        {
            DBHelp dbh = new DBHelp();
            DataTable dt = new DataTable();
            string cmd = "Profile_GetAll";
            dt = dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }

        //Ham lay ve ID nhan vao
        public static Profile GetInfo(int ID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@profile_id", ID);
            Profile profile = new Profile();
            string cmd = "Profile_GetInfo";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (sdr.Read())
            {
                profile.Profile_id = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    profile.User_id = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    profile.Rating = sdr.GetInt32(2);
                if (!sdr.IsDBNull(3))
                    profile.Nick_name = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    profile.Dob = sdr.GetDateTime(4);
                if (!sdr.IsDBNull(5))
                    profile.Gender = sdr.GetBoolean(5);
                if (!sdr.IsDBNull(6))
                    profile.About_me = sdr.GetString(6);
                if (!sdr.IsDBNull(7))
                    profile.Relationship = sdr.GetString(7);
                if (!sdr.IsDBNull(8))
                    profile.Looking_for = sdr.GetString(8);
                if (!sdr.IsDBNull(9))
                    profile.Phone = sdr.GetString(9);
                if (!sdr.IsDBNull(10))
                    profile.Interests = sdr.GetString(10);
                if (!sdr.IsDBNull(11))
                    profile.Education = sdr.GetString(11);
                if (!sdr.IsDBNull(12))
                    profile.Hobbies = sdr.GetString(12);
                if (!sdr.IsDBNull(13))
                    profile.Modified_date = sdr.GetDateTime(13);
            }
            return profile;
        }

        public static Profile GetInfoByUserID(int UserID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@user_id", UserID);
            Profile profile = new Profile();
            string cmd = "select * from tbl_profile where user_id = @user_id";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.Text);
            if (sdr.Read())
            {
                profile.Profile_id = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    profile.User_id = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    profile.Rating = sdr.GetInt32(2);
                if (!sdr.IsDBNull(3))
                    profile.Nick_name = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    profile.Dob = sdr.GetDateTime(4);
                if (!sdr.IsDBNull(5))
                    profile.Gender = sdr.GetBoolean(5);
                if (!sdr.IsDBNull(6))
                    profile.About_me = sdr.GetString(6);
                if (!sdr.IsDBNull(7))
                    profile.Relationship = sdr.GetString(7);
                if (!sdr.IsDBNull(8))
                    profile.Looking_for = sdr.GetString(8);
                if (!sdr.IsDBNull(9))
                    profile.Phone = sdr.GetString(9);
                if (!sdr.IsDBNull(10))
                    profile.Interests = sdr.GetString(10);
                if (!sdr.IsDBNull(11))
                    profile.Education = sdr.GetString(11);
                if (!sdr.IsDBNull(12))
                    profile.Hobbies = sdr.GetString(12);
                if (!sdr.IsDBNull(13))
                    profile.Modified_date = sdr.GetDateTime(13);
            }
            return profile;
        }

        //Them profile
        public static int Insert(Profile profile)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@user_id", profile.User_id);
            dbh.AddParameter("@rate_id", profile.Rating);
            dbh.AddParameter("@nick_name", profile.Nick_name);
            dbh.AddParameter("@dob", profile.Dob);
            dbh.AddParameter("@gender", profile.Gender);
            dbh.AddParameter("@about_me", profile.About_me);
            dbh.AddParameter("@relationship", profile.Relationship);
            dbh.AddParameter("@looking_for", profile.Looking_for);
            dbh.AddParameter("@phone", profile.Phone);
            dbh.AddParameter("@interests", profile.Interests);
            dbh.AddParameter("@education", profile.Education);
            dbh.AddParameter("@hobbies", profile.Hobbies);
            dbh.AddParameter("@modified_date", profile.Modified_date);

            SqlParameter ProfileID = new SqlParameter();
            ProfileID.DbType = DbType.Int32;
            ProfileID.Direction = ParameterDirection.Output;
            ProfileID.ParameterName = @"@profile_id";
            dbh.AddParameter(ProfileID);
            try
            {
                string cmd = "Profile_Insert";
                dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
                return (int)ProfileID.Value;
            }
            catch (System.Exception)
            {
                return -1;
            }
        }

        //Sua profile
        public static void Update(Profile profile)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@profile_id", profile.Profile_id);
            dbh.AddParameter("@user_id", profile.User_id);
            dbh.AddParameter("@rating", profile.Rating);
            dbh.AddParameter("@nick_name", profile.Nick_name);
            dbh.AddParameter("@dob", profile.Dob);
            dbh.AddParameter("@gender", profile.Gender);
            dbh.AddParameter("@about_me", profile.About_me);
            dbh.AddParameter("@relationship", profile.Relationship);
            dbh.AddParameter("@looking_for", profile.Looking_for);
            dbh.AddParameter("@phone", profile.Phone);
            dbh.AddParameter("@interests", profile.Interests);
            dbh.AddParameter("@education", profile.Education);
            dbh.AddParameter("@hobbies", profile.Hobbies);
            dbh.AddParameter("@modified_date", profile.Modified_date);
            string cmd = "Profile_Update";
            //string cmd = "update tbl_profile	set		user_id = @user_id,		rate_id = @rate_id,		nick_name = @nick_name,		dob = @dob,		gender = @gender,		about_me = @about_me,		relationship = @relationship,		looking_for = @looking_for,		phone = @phone,		interests =	@interests,		education = @education,		hobbies = @hobbies,		modified_date = @modified_date	where profile_id = @profile_id";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        //Xoa profile
        public static void Delete(int ID)
        {
            Console.WriteLine("begin");
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@profile_id", ID);
            string cmd = "Profile_Delete";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        // can thiet them sau
    }
}
